﻿using Microsoft.AspNetCore.Mvc;
using RxxAdmin.Common.HttpContextUser;
using RxxAdmin.Model.ViewModels;
using RxxAdmin.Model;
using RxxAdmin.Model.Entity;
using RxxAdmin.IServices;
using SqlSugar;
using RxxAdmin.Services;

namespace RxxAdmin.Api.Controllers.permission;
/// <summary>
/// 角色菜单
/// </summary>
public class SysRoleMenuController : BaseApiController
{
    private readonly ISysRoleMenuService _sysRoleMenuService;

    public SysRoleMenuController(ISysRoleMenuService sysRoleMenuService)
    {
        _sysRoleMenuService = sysRoleMenuService;
    }
    [HttpGet]
    public async Task<MessageModel<List<SysRoleMenu>>> GetRoleMenu([FromQuery] SysRoleMenuIn input)
    {
        var roleMenus = (await _sysRoleMenuService.Query()).Where(x => x.RoleId == input.RoleId).ToList();
        return Success(roleMenus);
    }
    [HttpPost]
    public async Task<MessageModel<string>> Add(SysRoleMenuIn input)
    {
        var roleMenus = (await _sysRoleMenuService.Query()).Where(x => x.RoleId == input.RoleId).ToList();
        var bo = await _sysRoleMenuService.DeleteByIds(Array.ConvertAll(roleMenus.Select(x => x.Id).ToArray(), Convert.ToString));
        foreach (var item in input.MenuIds)
        {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.RoleId = input.RoleId;
            sysRoleMenu.MenuId = item;
            var row = await _sysRoleMenuService.Add(sysRoleMenu);
        }
        return true ? Success("", "新增成功") : Failed("新增失败");
    }
}
